module.exports = () => {
    return async function auth(ctx, next) {
        const { authorization } = ctx.request.header;// 取出来 存放token的容器
        if (authorization) {
            const token = authorization.split(' ')[1] // 把token截取出来
            try {
                const jwt = require('jsonwebtoken')
                jwt.verify(token, ctx.app.config.keys) // 解析token  失败会报错  报错会被catch捕获到
                await next()
            } catch (err) {
                ctx.status = 401;
                ctx.body = {
                    success: false,
                    message: err.message + '无权限'
                }
            }
        } else {
            ctx.status = 401;
            ctx.body = {
                success: false,
                message: "请先登录"
            }
        }
    }
}
